package com.mycloud.dictionary.service;

import com.mycloud.util.FileUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: dell
 * Date: 12/14/11
 * Time: 3:49 PM
 * To change this template use File | Settings | File Templates.
 */
@Service
public class DictionaryDataProvider {

    @Autowired
    private DictionaryService dictionaryService;

    private Logger logger = Logger.getLogger(getClass());

    public void updateDatabse() {

        try {

            List<String> importedWords = dictionaryService.getAllWords();

            List<String> words = FileUtil.readFileAsList("C:\\workspaces\\enterprise\\dictionary\\resource\\data\\words.txt");
//            List<String> words = new ArrayList<String>();
//            words.add("soak");

            int imortedCount = (importedWords != null ? importedWords.size() : 0);
            for (String word : words) {

                int index = word.indexOf(" ");
                if (index == -1) {
                    index = word.length();
                }
                word = word.substring(0, index);

                if (importedWords.indexOf(word) != -1) {
                    continue;
                }


                String translation = dictionaryService.getFarsi123Translation(word);

                dictionaryService.persist(word, translation);

                imortedCount++;

                if (logger.isDebugEnabled()) {
                    logger.debug(word + " " + imortedCount + "/" + words.size());
                }

            }

        } catch (Throwable t) {
            t.printStackTrace();
        }

    }

    public void downloadVoices() throws IOException {

        List<String> words = dictionaryService.getAllWordsNotHavingAudioPath();

        logger.debug("words: " + (words != null ? words.size() : 0));

        int count = 0;
        int noAudio = 0;
        for (String word : words) {
            try {
                String rawTranslation = dictionaryService.getDatabaseTranslation(word);
                int start = rawTranslation.indexOf("audio_files");
                if (start != -1) {
                    int end = rawTranslation.indexOf(".mp3", start);
                    String audioPath = rawTranslation.substring(start + "audio_files/".length(),
                            end + ".mp3".length());
                    if (logger.isDebugEnabled()) {
                        logger.debug("audioPath [" + audioPath + "]");
                    }
                    dictionaryService.downloadAudio(audioPath);
                    dictionaryService.setAudioPath(word, audioPath);
                    count++;
                    logger.debug(word + ": " + count + " / " + words.size());
                }else{
                    noAudio++;
                    logger.debug("no audio file [" + word + "] " + noAudio + " / " + words.size());
                }
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }

    }

}
